<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('basic');
	$return['list']=array();
	set_html('title',$basic['title']?$basic['title']:'门店');
	$return['params']=array();
	$return['storeset']=$this->plugin_setting('storeset');
	$return['pcd']=$this->session('pcd')?:array('city'=>'');
	$return['lat_lng']=$this->session('lat_lng')?:'';
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	if($_RQ['city'] || $_RQ['nocity']){
		$pcd=array('province'=>$_RQ['province'],'city'=>$_RQ['city'],'district'=>$_RQ['district'],'street'=>$_RQ['street']);
		$this->session('pcd',$pcd);
	}
	$lat_lng=$this->session('lat_lng')?:array('lat'=>'31.235929','lng'=>'121.480539');
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$pindex = max(1, 1);
	$psize = 500;
	$total='';
	$fields=array('id','logo','title','address','mobile','onlinetime','lat_lng','pcd','out_juli','out_pay');
	$list=pdo_getslice('jy_weishop_store',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as $k=>&$v){
		$v['logo']=tomedia($v['logo']);
		$v['pcd']=json_decode($v['pcd'],true);
		if(trim($_RQ['province']) && $v['pcd']['province']!=trim($_RQ['province'])){
			unset($list[$k]);
			continue;
		}
		if(trim($_RQ['city']) && $v['pcd']['city']!=trim($_RQ['city'])){
			unset($list[$k]);
			continue;
		}
		if(trim($_RQ['district']) && $v['pcd']['district']!=trim($_RQ['district'])){
			unset($list[$k]);
			continue;
		}
		$v['lat_lng']=json_decode($v['lat_lng'],true);
		$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
		$v['juli']=intval($v['juli']/1000);
		$v['onlinetime']=json_decode($v['onlinetime'],true);
		if($v['juli']<10){
			$v['peisongtime']=$v['juli']*8+38;
		}else if($v['juli']<30){
			$v['peisongtime']=$v['juli']*3+38;
		}else{
			$v['peisongtime']=$v['juli']*1+38;
		}
		$v['out_juli']=$v['out_juli']>0?$v['out_juli']:0;
		if($v['id']==$_SESSION['store_id']){
			$newstore=$v;
			unset($list[$k]);
		}
	}
	paixu($list,'juli');
	if($newstore){
		array_unshift($list,$newstore);
	}
	if($_RQ['limit']){
		$list=array_slice($list,0,intval($_RQ['limit']));
	}else{
		$page=intval($_RQ['page'])?:1;
		$start=($page-1)*10;
		$list=array_slice($list,$start,10);
	}
	$return['list']=$list;
	if($_RQ['islocation'] && !$_SESSION['store_id']){
		$_SESSION['store_id']=$list[0]['id'];
	}
	exi($return);
}
if($_RQ['op']=='get_juli'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
	}
	$store_id=intval($_RQ['store_id'])?:$_SESSION['store_id'];
	$storelat_lng=pdo_getcolumn('jy_weishop_store',array('id'=>$store_id),'lat_lng');
	$storelat_lng=json_decode($storelat_lng,true);
	$juli=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$storelat_lng['lng'],$storelat_lng['lat']);
	$juli=intval($juli/1000);
	$juli=$juli?:0;
	exi($juli);
}
if($_RQ['op']=='out_time'){
	$store_id=intval($_RQ['store_id'])?:$_SESSION['store_id'];
	$merch_id=intval($_RQ['merch_id']);
	$citypeisong=$this->plugin_setting('citypeisong');
	$post['set_timebox']=$citypeisong['set_timebox'];
	$post['peisong_timebox']=$citypeisong['peisong_timebox'];
	$post['out_pay']=$_RQ['out_pay'];
	if($merch_id){
		$out_time = model('jy_merch/model')->out_times($merch_id,$post);
	}else{
		$out_time = model('jy_weishop/store')->out_times($store_id,$post);
	}
	$timestamp = TIMESTAMP + 60 * 30;
	$data = model('jy_weishop/store')->array_order($timestamp, $out_time['timestamp']);
	$predict_index = array_search($data, $out_time['timestamp']);
	$predict_day = $out_time['days'][0];
	$predict_time = $out_time['times'][$predict_index]['start'].'~'.$out_time['times'][$predict_index]['end'];
	$text_time = "尽快送达";
	$return['timestamp']=$timestamp;
	$return['predict_time']=$predict_time;
	$return['out_time']=$out_time;
	exi($return);
}
if($_RQ['op']=='get_work_time'){
	$store_id=intval($_RQ['store_id'])?:$_SESSION['store_id'];
	$store=pdo_get('jy_weishop_store',array('id'=>$store_id));
	$onlinetime=json_decode($store['onlinetime'],true);
	$daytime=date('Y-m-d');
	if($onlinetime['start']){
		$start=$daytime.' '.$onlinetime['start'];
		$start=strtotime($start);
	}else{
		$start = mktime(8, 0);
	}
	if($onlinetime['end']){
		$end=$daytime.' '.$onlinetime['end'];
		$end=strtotime($end);
	}else{
		$end = mktime(22, 0);
	}
	$times=array();
	for($i = $start; $i < $end;) {
			$times[] = array(
						'start' => date('H:i', $i),
						'end' => date('H:i', $i + 1800),
						'timestamp' => $i + 1800,
						'fee' => 0,
						'timev' =>date('H:i', $i).'~'.date('H:i', $i + 1800),
			);
			$i += 1800;
	}
	$work_time=str_array($store['work_time']);
	//门店营业时间
	//设置日期
	$data=array();
	$weekarray=array('星期日','星期一','星期二','星期三','星期四','星期五','星期六'); //先定义一个数组
	for($i=0;$i<15;$i++){
		$weekid=date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))));
		if(count($work_time)){
			$iswork=strpos($store['work_time'],';'.$weekid.';')?1:0;
		}else{
			$iswork=1;
		}
		$data[]=array(
			'riqi'=>date("m-d",strtotime("+".$i." day")),
			'day'=>date("Y-m-d",strtotime("+".$i." day")),
			'xinq'=>$weekarray[$weekid],
			'iswork'=>$iswork,
		);
	}
	$return=array('days'=>$data,'hours'=>$times);
	exi($return);
}
if($_RQ['op']=='uppcd'){
	$post=get_postdata();
	if($post['lat'] || $post['lng']){
		$lat_lng=array('lat'=>$post['lat'],'lng'=>$post['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$this->session('pcd',$post);
	exi('更新成功！');
}
?>